<?php
namespace app\common\controller;

/**
 * 更新积分类
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/27
 * Time: 11:18
 */
class SaveIntegral {

    /**
     * 获取积分信息
     * @return mixed
     */
    public function get_integral(){
        //获取积分信息
        $maps = [];
        $maps[] = ['key','=','integral'];
        $conf_info = model('conf')->get_list($maps);
        $conf_info_new = [];
        foreach ($conf_info as $k => $v) {
            $conf_info_new[$v['key']] = $v['value'];
        }
        $integral_arr = json_decode($conf_info_new['integral'],true);
        return $integral_arr;
    }

    /**
     * 更新用户总积分和可用积分
     * @param $openid
     * @return int
     */
    public function save_user_integral($openid){
        //计算用户总积分和可用积分
        $cui = new CountUserIntegral();
        $integral_arr = $cui->count_user_integral($openid);

        $maps = [];
        $maps[] = ['openid','=',$openid];

        $u_data = [];
        $u_data['total_integral'] = $integral_arr['total_integral'];
        $r = model('user')->where($maps)->update($u_data);
        return 1;
    }

    /**
     * 更新社区巡店积分
     * @param $cp_branchid
     * @return int
     */
    public function save_cp_integral($cp_branchid){
        $maps = [];
        $maps[] = ['id','=',$cp_branchid];
        $cb_detail = model('cp_branch')->get_detail($maps,'id,time_code');
        if(!empty($cb_detail) && $cb_detail['time_code'] != 'other'){
            $integral_arr = $this->get_integral();
            $integral = isset($integral_arr['community_patrol']) ? $integral_arr['community_patrol'] : 0;

            //新增用户积分流水
            $dir = new DealIntegralRecord();

            $maps = [];
            $maps[] = ['cp_branchid','=',$cb_detail['id']];
            $maps[] = ['status','=',1];
            $openid_arr = model('cp_floor')->get_column($maps,'DISTINCT openid');
            foreach($openid_arr as $k => $v){
                $r = $dir->add_integral_record($v,$cb_detail['id'], 'community_patrol',$integral);

                //更新用户总积分和可用积分
                $r = $this->save_user_integral($v);
            }
        }
        return 1;
    }

    /**
     * 更新未完成社区巡店积分
     * @param $cp_branchid
     * @return int
     */
    public function save_no_cp_integral($cp_branchid){
        $maps = [];
        $maps[] = ['id','=',$cp_branchid];
        $cb_detail = model('cp_branch')->get_detail($maps,'id,branchid,time_code');
        if(!empty($cb_detail) && $cb_detail['time_code'] != 'other'){
            $integral_arr = $this->get_integral();
            $integral = isset($integral_arr['no_community_patrol']) ? $integral_arr['no_community_patrol'] : 0;

            //新增用户积分流水
            $dir = new DealIntegralRecord();

            //查找分部名称
            $maps = [];
            $maps[] = ['id','=',$cb_detail['branchid']];
            $b_detail = model('branch')->get_detail($maps,'id,name');

            //根据分部名称查找用户
            $maps = [];
            $maps[] = ['department','=',$b_detail['name']];
            $openid_arr = model('user')->get_column($maps,'openid');
            foreach($openid_arr as $k => $v){
                $r = $dir->add_integral_record($v,$cb_detail['id'], 'no_community_patrol',$integral);

                //更新用户总积分和可用积分
                $r = $this->save_user_integral($v);
            }
        }
        return 1;
    }

    /**
     * 更新日常巡店积分
     * @param $cp_floorid
     * @return int
     */
    public function save_dp_integral($cp_floorid){
        $maps = [];
        $maps[] = ['id','=',$cp_floorid];
        $cf_detail = model('cp_floor')->get_detail($maps,'id,openid');
        if(!empty($cf_detail)){
            $integral_arr = $this->get_integral();
            $integral = isset($integral_arr['day_patrol']) ? $integral_arr['day_patrol'] : 0;

            //新增用户积分流水
            $dir = new DealIntegralRecord();
            $r = $dir->add_integral_record($cf_detail['openid'],$cf_detail['id'], 'day_patrol',$integral);

            //更新用户总积分和可用积分
            $r = $this->save_user_integral($cf_detail['openid']);
        }
        return 1;
    }

    /**
     * 更新创建事项积分
     * @param $matterid
     * @return int
     */
    public function save_cm_integral($matterid){
        $maps = [];
        $maps[] = ['id','=',$matterid];
        $m_detail = model('matter')->get_detail($maps,'id,from_openid');
        if(!empty($m_detail)){
            $integral_arr = $this->get_integral();
            $integral = isset($integral_arr['create_matter']) ? $integral_arr['create_matter'] : 0;

            //新增用户积分流水
            $dir = new DealIntegralRecord();
            $r = $dir->add_integral_record($m_detail['from_openid'],$m_detail['id'], 'create_matter',$integral);

            //更新用户总积分和可用积分
            $r = $this->save_user_integral($m_detail['from_openid']);
        }
        return 1;
    }

    /**
     * 更新参与事项积分
     * @param $matterid
     * @return int
     */
    public function save_jm_integral($matterid){
        $maps = [];
        $maps[] = ['id','=',$matterid];
        $m_detail = model('matter')->get_detail($maps,'id,from_openid');
        if(!empty($m_detail)){
            $integral_arr = $this->get_integral();
            $integral = isset($integral_arr['join_matter']) ? $integral_arr['join_matter'] : 0;

            //新增用户积分流水
            $dir = new DealIntegralRecord();

            $maps = [];
            $maps[] = ['matterid','=',$m_detail['id']];
            $maps[] = ['user_openid','<>',$m_detail['from_openid']];
            $user_openid_arr = model('matter_flow')->get_column($maps,'DISTINCT user_openid');
            foreach($user_openid_arr as $k => $v){
                $r = $dir->add_integral_record($v,$m_detail['id'], 'join_matter',$integral);

                //更新用户总积分和可用积分
                $r = $this->save_user_integral($v);
            }
        }
        return 1;
    }

    /**
     * 更新超时事项积分
     * @param $openid
     * @param $matterid
     * @return int
     */
    public function save_otm_integral($openid,$matterid){
        $integral_arr = $this->get_integral();
        $integral = isset($integral_arr['out_time_matter']) ? $integral_arr['out_time_matter'] : 0;

        //新增用户积分流水
        $dir = new DealIntegralRecord();
        $r = $dir->add_integral_record($openid,$matterid, 'out_time_matter',$integral);

        //更新用户总积分和可用积分
        $r = $this->save_user_integral($openid);

        return 1;
    }

    /**
     * 更新批复抄送积分
     * @param $copyid
     * @return int
     */
    public function save_rc_integral($copyid){
        $maps = [];
        $maps[] = ['id','=',$copyid];
        $c_detail = model('copy')->get_detail($maps,'id,copy_openid');
        if(!empty($c_detail)){
            $integral_arr = $this->get_integral();
            $integral = isset($integral_arr['reply_copy']) ? $integral_arr['reply_copy'] : 0;

            //新增用户积分流水
            $dir = new DealIntegralRecord();
            $r = $dir->add_integral_record($c_detail['copy_openid'],$c_detail['id'], 'reply_copy',$integral);

            //更新用户总积分和可用积分
            $r = $this->save_user_integral($c_detail['copy_openid']);
        }
        return 1;
    }

    /**
     * 更新发布推广工作积分
     * @param $wreid
     * @return int
     */
    public function save_re_integral($wreid){
        $maps = [];
        $maps[] = ['id','=',$wreid];
        $wre_detail = model('work_record_extend')->get_detail($maps,'id,openid');
        if(!empty($wre_detail)){
            $integral_arr = $this->get_integral();
            $integral = isset($integral_arr['release_extend']) ? $integral_arr['release_extend'] : 0;

            //新增用户积分流水
            $dir = new DealIntegralRecord();
            $r = $dir->add_integral_record($wre_detail['openid'],$wre_detail['id'], 'release_extend',$integral);

            //更新用户总积分和可用积分
            $r = $this->save_user_integral($wre_detail['openid']);
        }
        return 1;
    }

    /**
     * 更新未发布推广工作积分
     * @param $openid
     * @param $sourceid
     * @return int
     */
    public function save_no_re_integral($openid,$sourceid){
        $integral_arr = $this->get_integral();
        $integral = isset($integral_arr['no_release_extend']) ? $integral_arr['no_release_extend'] : 0;

        //新增用户积分流水
        $dir = new DealIntegralRecord();
        $r = $dir->add_integral_record($openid,$sourceid, 'no_release_extend',$integral);

        //更新用户总积分和可用积分
        $r = $this->save_user_integral($openid);
        return 1;
    }

    /**
     * 更新发布工作日志积分
     * @param $wrdid
     * @return int
     */
    public function save_rd_integral($wrdid){
        $maps = [];
        $maps[] = ['id','=',$wrdid];
        $wrd_detail = model('work_record_daily')->get_detail($maps,'id,openid');
        if(!empty($wrd_detail)){
            $integral_arr = $this->get_integral();
            $integral = isset($integral_arr['release_daily']) ? $integral_arr['release_daily'] : 0;

            //新增用户积分流水
            $dir = new DealIntegralRecord();
            $r = $dir->add_integral_record($wrd_detail['openid'],$wrd_detail['id'], 'release_daily',$integral);

            //更新用户总积分和可用积分
            $r = $this->save_user_integral($wrd_detail['openid']);
        }
        return 1;
    }

    /**
     * 更新发布活动开展积分
     * @param $wraid
     * @return int
     */
    public function save_ra_integral($wraid){
        $maps = [];
        $maps[] = ['id','=',$wraid];
        $wra_detail = model('work_record_active')->get_detail($maps,'id,openid');
        if(!empty($wra_detail)){
            $integral_arr = $this->get_integral();
            $integral = isset($integral_arr['release_active']) ? $integral_arr['release_active'] : 0;

            //新增用户积分流水
            $dir = new DealIntegralRecord();
            $r = $dir->add_integral_record($wra_detail['openid'],$wra_detail['id'], 'release_active',$integral);

            //更新用户总积分和可用积分
            $r = $this->save_user_integral($wra_detail['openid']);
        }
        return 1;
    }

    /**
     * 更新发布通知公告积分
     * @param $wrnid
     * @return int
     */
    public function save_rn_integral($wrnid){
        $maps = [];
        $maps[] = ['id','=',$wrnid];
        $wrn_detail = model('work_record_notice')->get_detail($maps,'id,openid');
        if(!empty($wrn_detail)){
            $integral_arr = $this->get_integral();
            $integral = isset($integral_arr['release_notice']) ? $integral_arr['release_notice'] : 0;

            //新增用户积分流水
            $dir = new DealIntegralRecord();
            $r = $dir->add_integral_record($wrn_detail['openid'],$wrn_detail['id'], 'release_notice',$integral);

            //更新用户总积分和可用积分
            $r = $this->save_user_integral($wrn_detail['openid']);
        }
        return 1;
    }

    /**
     * 更新发布每日数据积分
     * @param $wrdid
     * @return int
     */
    public function save_r_data_integral($wrdid){
        $maps = [];
        $maps[] = ['id','=',$wrdid];
        $wrd_detail = model('work_record_data')->get_detail($maps,'id,openid');
        if(!empty($wrd_detail)){
            $integral_arr = $this->get_integral();
            $integral = isset($integral_arr['release_data']) ? $integral_arr['release_data'] : 0;

            //新增用户积分流水
            $dir = new DealIntegralRecord();
            $r = $dir->add_integral_record($wrd_detail['openid'],$wrd_detail['id'], 'release_data',$integral);

            //更新用户总积分和可用积分
            $r = $this->save_user_integral($wrd_detail['openid']);
        }
        return 1;
    }

    /**
     * 更新发布日记账积分
     * @param $wraid
     * @return int
     */
    public function save_r_account_integral($wraid){
        $maps = [];
        $maps[] = ['id','=',$wraid];
        $wra_detail = model('work_record_account')->get_detail($maps,'id,openid');
        if(!empty($wra_detail)){
            $integral_arr = $this->get_integral();
            $integral = isset($integral_arr['release_account']) ? $integral_arr['release_account'] : 0;

            //新增用户积分流水
            $dir = new DealIntegralRecord();
            $r = $dir->add_integral_record($wra_detail['openid'],$wra_detail['id'], 'release_account',$integral);

            //更新用户总积分和可用积分
            $r = $this->save_user_integral($wra_detail['openid']);
        }
        return 1;
    }

    /**
     * 更新发布工作圈积分
     * @param $wcmid
     * @return int
     */
    public function save_r_circle_integral($wcmid){
        $maps = [];
        $maps[] = ['id','=',$wcmid];
        $wcm_detail = model('work_circle_msg')->get_detail($maps,'id,openid');
        if(!empty($wcm_detail)){
            $integral_arr = $this->get_integral();
            $integral = isset($integral_arr['release_circle']) ? $integral_arr['release_circle'] : 0;

            //新增用户积分流水
            $dir = new DealIntegralRecord();
            $r = $dir->add_integral_record($wcm_detail['openid'],$wcm_detail['id'], 'release_circle',$integral);

            //更新用户总积分和可用积分
            $r = $this->save_user_integral($wcm_detail['openid']);
        }
        return 1;
    }

    /**
     * 更新点赞及被点赞工作圈积分
     * @param $wccid
     * @return int
     */
    public function save_ct_integral($wccid){
        $maps = [];
        $maps[] = ['id','=',$wccid];
        $wcc_detail = model('work_circle_comment')->get_detail($maps,'id,openid,wcmid');
        if(!empty($wcc_detail)){
            $integral_arr = $this->get_integral();
            $dir = new DealIntegralRecord();

            //点赞
            //新增用户积分流水
            $integral = isset($integral_arr['circle_thumbs']) ? $integral_arr['circle_thumbs'] : 0;
            $r = $dir->add_integral_record($wcc_detail['openid'],$wcc_detail['id'], 'circle_thumbs',$integral);

            //更新用户总积分和可用积分
            $r = $this->save_user_integral($wcc_detail['openid']);

            //被点赞
            $maps = [];
            $maps[] = ['id','=',$wcc_detail['wcmid']];
            $wcm_detail = model('work_circle_msg')->get_detail($maps,'id,openid');

            //新增用户积分流水
            $integral = isset($integral_arr['circle_be_thumbs']) ? $integral_arr['circle_be_thumbs'] : 0;
            $r = $dir->add_integral_record($wcm_detail['openid'],$wcc_detail['id'], 'circle_be_thumbs',$integral);

            //更新用户总积分和可用积分
            $r = $this->save_user_integral($wcm_detail['openid']);
        }
        return 1;
    }

    /**
     * 更新评论及被评论工作圈积分
     * @param $wccid
     * @return int
     */
    public function save_cc_integral($wccid){
        $maps = [];
        $maps[] = ['id','=',$wccid];
        $wcc_detail = model('work_circle_comment')->get_detail($maps,'id,openid,wcmid');
        if(!empty($wcc_detail)){
            $integral_arr = $this->get_integral();
            $dir = new DealIntegralRecord();

            //评论
            //新增用户积分流水
            $integral = isset($integral_arr['circle_comment']) ? $integral_arr['circle_comment'] : 0;
            $r = $dir->add_integral_record($wcc_detail['openid'],$wcc_detail['id'], 'circle_comment',$integral);

            //更新用户总积分和可用积分
            $r = $this->save_user_integral($wcc_detail['openid']);

            //被评论
            $maps = [];
            $maps[] = ['id','=',$wcc_detail['wcmid']];
            $wcm_detail = model('work_circle_msg')->get_detail($maps,'id,openid');

            //新增用户积分流水
            $integral = isset($integral_arr['circle_be_comment']) ? $integral_arr['circle_be_comment'] : 0;
            $r = $dir->add_integral_record($wcm_detail['openid'],$wcc_detail['id'], 'circle_be_comment',$integral);

            //更新用户总积分和可用积分
            $r = $this->save_user_integral($wcm_detail['openid']);
        }
        return 1;
    }

    /**
     * 更新小课堂学习积分
     * @param $clcid
     * @return int
     */
    public function save_cl_integral($clcid){
        $maps = [];
        $maps[] = ['id','=',$clcid];
        $clc_detail = model('class_learn_ci')->get_detail($maps,'id,openid,score');
        if(!empty($clc_detail)){
            $integral = $clc_detail['score'];

            //新增用户积分流水
            $dir = new DealIntegralRecord();
            $r = $dir->add_integral_record($clc_detail['openid'],$clc_detail['id'], 'class_learn',$integral);

            //更新用户总积分和可用积分
            $r = $this->save_user_integral($clc_detail['openid']);
        }
        return 1;
    }

    /**
     * 更新写备忘录积分
     * @param $crwid
     * @return int
     */
    public function save_wr_integral($crwid){
        $maps = [];
        $maps[] = ['id','=',$crwid];
        $crw_detail = model('cp_region_ws')->get_detail($maps,'id,openid,remark');
        if(!empty($crw_detail) && !empty($crw_detail['remark'])){
            $integral_arr = $this->get_integral();
            $integral = isset($integral_arr['write_remark']) ? $integral_arr['write_remark'] : 0;

            //新增用户积分流水
            $dir = new DealIntegralRecord();
            $r = $dir->add_integral_record($crw_detail['openid'],$crw_detail['id'], 'write_remark',$integral);

            //更新用户总积分和可用积分
            $r = $this->save_user_integral($crw_detail['openid']);
        }
        return 1;
    }

    /**
     * 更新积分操作积分
     * @param $ioid
     * @return int
     */
    public function save_io_integral($ioid){
        $maps = [];
        $maps[] = ['id','=',$ioid];
        $io_detail = model('integral_operate')->get_detail($maps,'id,openid,integral');
        if(!empty($io_detail)){
            $integral = $io_detail['integral'];

            //新增用户积分流水
            $dir = new DealIntegralRecord();
            $r = $dir->add_integral_record($io_detail['openid'],$io_detail['id'], 'admin_operate',$integral);

            //更新用户总积分和可用积分
            $r = $this->save_user_integral($io_detail['openid']);
        }
        return 1;
    }
}